package com.tianbao54.thrift.server;

import com.tianbao54.thrift.generated.UserService;
import com.tianbao54.thrift.impl.UserServiceImpl;
import org.apache.thrift.TProcessorFactory;
import org.apache.thrift.protocol.TCompactProtocol;
import org.apache.thrift.server.THsHaServer;
import org.apache.thrift.server.TServer;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TNonblockingServerSocket;
import org.apache.thrift.transport.TTransportException;

public class ThriftServer {

    public static void main(String[] args) {

        try {
            TNonblockingServerSocket serverSocket = new TNonblockingServerSocket(8888);
            THsHaServer.Args haArgs = new THsHaServer.Args(serverSocket);
            UserService.Processor<UserServiceImpl> processor = new UserService.Processor<>(new UserServiceImpl());

            haArgs.protocolFactory(new TCompactProtocol.Factory());
            haArgs.transportFactory(new TFramedTransport.Factory());
            haArgs.processorFactory(new TProcessorFactory(processor));

            TServer server = new THsHaServer(haArgs);
            System.out.println("thrift server is started");
            server.serve();
        } catch (TTransportException e) {
            e.printStackTrace();
        }
    }
}
